package com.hao.study.controller;

import com.github.pagehelper.PageInfo;
import com.hao.study.common.APIResponse;
import com.hao.study.common.ResponeCode;
import com.hao.study.entity.OpLog;
import com.hao.study.service.OpLogService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author: author
 * @description:  用于操作和登录日志
 * @date: 2024/6/13 9:25
 * @version: 1.0
 */
@Api(tags={"操作登录日志管理接口"})
@RestController
@RequestMapping("/opLog")
public class OpLogController {
    @Autowired
    OpLogService service;

    @GetMapping("/list")
    public APIResponse list(@RequestParam(defaultValue = "1")Integer pageNum,
                            @RequestParam(defaultValue = "10")Integer pageSize,
                            String keyword){
        PageInfo<OpLog> list = service.getOpLogList(pageNum,pageSize,keyword);
        return new APIResponse<>(ResponeCode.SUCCESS,"查询成功",list);
    }


    @GetMapping("/loginLogList")
    public APIResponse loginLogList(@RequestParam(defaultValue = "1")Integer pageNum,
                                    @RequestParam(defaultValue = "10")Integer pageSize,
                                    String keyword){
        PageInfo<OpLog> loginLogList=service.loginLogList(pageNum,pageSize,keyword);
        return new APIResponse<>(ResponeCode.SUCCESS,"查询成功",loginLogList);
    }

    @PostMapping("/create")
    @Transactional
    public APIResponse create(OpLog opLog){
        service.createOpLog(opLog);
        return new APIResponse<>(ResponeCode.SUCCESS,"创建成功");
    }

    @PostMapping("/delete")
    public APIResponse delete(String ids){
        System.out.println("ids===" + ids);
        // 批量删除
        String[] arr = ids.split(",");
        for (String id : arr) {
            service.deleteOpLog(id);
        }
        return new APIResponse(ResponeCode.SUCCESS, "删除成功");
    }

    @PostMapping("/update")
    @Transactional
    public APIResponse update(OpLog opLog) throws IOException {
        service.updateOpLog(opLog);
        return new APIResponse(ResponeCode.SUCCESS, "更新成功");
    }
}
